package com.example.springboot_shopsystem01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot_shopsystem01.dao.SupplierDao;
import com.example.springboot_shopsystem01.domain.Supplier;
import com.example.springboot_shopsystem01.service.SupplierService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierDao supplierDao;

    @Override
    public Boolean save(Supplier supplier) {
        return supplierDao.insert(supplier) > 0;  //返回的数大于零就成功
    }

    @Override
    public Boolean update(Supplier supplier) {
        return supplierDao.updateById(supplier) > 0;
    }

    @Override
    public Boolean delete(Integer id) {
        return supplierDao.deleteById(id) > 0;
    }

    @Override
    public Supplier getById(Integer id) {
        return supplierDao.selectById(id);
    }

    @Override
    public List<Supplier> getAll() {
        return supplierDao.selectList(null);
    }

    @Override
    public IPage<Supplier> getPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage, pageSize);
        supplierDao.selectPage(page, null);
        return page;
    }

    @Override
    public IPage<Supplier> getPage(int currentPage, int pageSize, Supplier supplier) {
        //增加条件查询
        LambdaQueryWrapper<Supplier> lqw = new LambdaQueryWrapper<Supplier>();
        lqw.like(Strings.isNotEmpty(supplier.getName()), Supplier::getName, supplier.getName());
        //lqw.like(Strings.isNotEmpty(supplier.getLevel()), Staff::getLevel, supplier.getLevel());
        lqw.like(Strings.isNotEmpty(supplier.getTelephone()), Supplier::getTelephone, supplier.getTelephone());
        lqw.like(supplier.getId() != null, Supplier::getId, supplier.getId());

        //添加到page中
        IPage page = new Page(currentPage, pageSize);
        supplierDao.selectPage(page, lqw);
        return page;
    }
}
